#include "historymessagedialog.h"
#include <QPushButton>
#include <QLabel>
#include <QScrollArea>
#include <QScrollBar>
#include <QThread>
#include "debug.h"
#include "model/datacenter.h"
#include "messageshowarea.h"


using model::MessageType;

HistoryMessageItem::HistoryMessageItem(const model::Message& msg)
{

    QGridLayout* layout = new QGridLayout(this);
    //layout->setVerticalSpacing(0);
    layout->setSpacing(0);


    QPushButton* avatarBtn = new QPushButton;
    avatarBtn->setStyleSheet("QPushButton{border:none;};");
    avatarBtn->setIcon(msg.sender.avatar);
    avatarBtn->setFixedSize(40,40);
    avatarBtn->setIconSize(QSize(40,40));

    QLabel* nameLabel = new QLabel;
    nameLabel->setFixedHeight(20);
    nameLabel->setText(msg.sender.nickname+"|"+msg.time);

    //消息体标签
    QWidget* msgLabel;    
    switch(msg.messageType)
    {
        case MessageType::TEXT_TYPE:
        {
            // QLabel* contentLabel = new QLabel;
            // contentLabel->setText(msg.content);
            // contentLabel->setWordWrap(true);
            // contentLabel->adjustSize();
            // msgLabel = contentLabel;
            msgLabel= new MessageContentLabel(msg.content,msg.messageType,true,msg.fileId,msg.content);
            break;
        }
        case MessageType::IMAGE_TYPE:
        {
            msgLabel = new MessageImageLabel(true,msg.fileId,msg.content);
            break;
        }
        case MessageType::FILE_TYPE:
        {
            msgLabel= new MessageContentLabel("[文件]"+msg.fileName,msg.messageType,true,msg.fileId,msg.content);
            break;
        }
        case MessageType::SPEECH_TYPE:
        {
            msgLabel= new MessageContentLabel("[语音]",msg.messageType,true,msg.fileId,msg.content);
            break;
        }
        default:
        {
            LOG()<<"消息类型错误，MessageType:"<<(size_t)msg.messageType;
        }
    }

    QPushButton* b1 = new QPushButton;
    b1->setFixedSize(40,40);
    QPushButton* b2 = new QPushButton;
    b2->setFixedSize(40,40);
    layout->addWidget(avatarBtn,0,0,2,1);
    layout->addWidget(nameLabel,0,1,1,1);
    layout->addWidget(msgLabel,1,1,5,1);
}



HistoryMessageDialog::HistoryMessageDialog(QWidget* parent)
{
    //1.设置属性
    this->setWindowTitle("历史消息");
    this->setFixedSize(550,550);
    this->setWindowIcon(QIcon(":/myResource/Image/WeChat.svg"));
    this->setAttribute(Qt::WA_DeleteOnClose);
    this->setStyleSheet("QWidget{background-color:white;}");

    //2.设置布局管理器
    QGridLayout* layout = new QGridLayout(this);

    //3.增添单选控件
    keySearchBtn = new QRadioButton;
    keySearchBtn->setText("按照关键词搜索");
        //默认是关键词搜索
    keySearchBtn->setChecked(true);

    timeSearchBtn = new QRadioButton;
    timeSearchBtn->setText("按照时间搜索");

    layout->addWidget(keySearchBtn,0,0,1,2);
    layout->addWidget(timeSearchBtn,0,2,1,2);

    //4.关键词搜索部分

    searchEdit = new QLineEdit;
    searchEdit->setPlaceholderText("要搜索的关键字");
    searchEdit->setFixedHeight(40);

    QPushButton* searchBtn = new QPushButton;
    searchBtn->setFixedSize(35,35);
    searchBtn->setIcon(QIcon(":/myResource/Image/search.svg"));
    searchBtn->setIconSize(QSize(25,25));

    layout->addWidget(searchEdit,1,0,1,8);
    layout->addWidget(searchBtn,1,8,1,1);

    //5.时间搜索部分(初始隐藏)
    QLabel* begTag = new QLabel;
    begTag->setText("开始时间");
    QLabel* endTag = new QLabel;
    endTag->setText("结束时间");

    begTimeEdit = new QDateTimeEdit;
    begTimeEdit->setFixedHeight(40);
    endTimeEdit = new QDateTimeEdit;
    endTimeEdit->setFixedHeight(40);

    begTag->hide();
    endTag->hide();
    begTimeEdit->hide();
    endTimeEdit->hide();

    //6.共同的滚动消息列表部分


    QScrollArea* scrollArea = new QScrollArea;
    scrollArea->setWidgetResizable(true);
    scrollArea->horizontalScrollBar()->setStyleSheet("QScrollBar:horizontal{height:0;}");
    scrollArea->verticalScrollBar()->setStyleSheet("QScrollBar:vertical{width:3px;}");


    QWidget*  container = new QWidget;
    scrollArea->setWidget(container);

    vLayout = new QVBoxLayout(container);
    vLayout->setSpacing(0);
    vLayout->setContentsMargins(0,0,0,0);

    layout->addWidget(scrollArea,2,0,1,9);

    //7.设置信号和槽
    connect(keySearchBtn,&QRadioButton::clicked,this,[=]()
    {
        layout->removeWidget(begTag);
        layout->removeWidget(endTag);
        layout->removeWidget(begTimeEdit);
        layout->removeWidget(endTimeEdit);
        begTag->hide();
        endTag->hide();
        begTimeEdit->hide();
        endTimeEdit->hide();

        layout->addWidget(searchEdit,1,0,1,8);
        searchEdit->show();
    });
    connect(timeSearchBtn,&QRadioButton::clicked,this,[=]()
    {
        layout->removeWidget(searchEdit);
        searchEdit->hide();


        layout->addWidget(begTag,1,0,1,1);
        layout->addWidget(begTimeEdit,1,1,1,3);
        layout->addWidget(endTag,1,4,1,1);
        layout->addWidget(endTimeEdit,1,5,1,3);
        begTag->show();
        endTag->show();
        begTimeEdit->show();
        endTimeEdit->show();
    });
    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    connect(searchBtn,&QPushButton::clicked,this,[=]()
    {
        connect(dataCenter,&model::DataCenter::getSearchHistoryListDone,this,[=]()
        {
            clear();
            for(auto& m:(*dataCenter->getSearchHistoryList()))
                addItem(m);
        });
        if(keySearchBtn->isChecked())
            dataCenter->getSearchHistoryListAsync(searchEdit->text());
        else
            dataCenter->getSearchHistoryListByTimeAsync(begTimeEdit->dateTime(),endTimeEdit->dateTime());

    });

#if TEST_UI
    for(int i =20;i>=0;i--)
    {
        model::UserInfo user;
        user.avatar =QIcon(":/myResource/Image/dogAvatar.svg");
        user.nickname = "张三"+QString::number(i);
        model::Message msg = model::Message::makeMessage(MessageType::TEXT_TYPE,"cid"+QString::number(i)
                                         ,user,"刘波六十六","");
        addItem(msg);
    }
    clear();
    for(int i =0;i<20;i++)
    {
        model::UserInfo user;
        user.avatar =QIcon(":/myResource/Image/dogAvatar.svg");
        user.nickname = "张三"+QString::number(i);
        const QString con = "阿贾克斯带回家客户阿是基督教基督教啊黑科基督教啊黑科基督教啊黑科啊黑科技啊实打实大师阿萨德阿萨";
        model::Message msg = model::Message::makeMessage(MessageType::TEXT_TYPE,"cid"+QString::number(i)
                                                         ,user,con.toUtf8(),"");
        addItem(msg);
    }
#endif

}
void HistoryMessageDialog::addItem(const model::Message& msg)
{
    HistoryMessageItem* item = new HistoryMessageItem(msg);
    vLayout->addWidget(item);
}
void HistoryMessageDialog::clear()
{
    int row = 0, col = 0;
    for(int i =vLayout->count()-1;i>=0;i--)
    {
        auto item = vLayout->takeAt(i);
        if(item&&item->widget())
        {
            QWidget* widget = item->widget();
            vLayout->removeWidget(item->widget());
            delete item->widget();

        }
    }

}
